﻿using System;
using System.Data;
using System.Collections.Generic;
using Entities;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using ATM.Utility.Helper;
using ATM.Utility.Constants;

namespace DataAccess
{
    public class CardDao
    {
        private static CardDao install;
        public static CardDao Install
        {
            get
            {
                if (install == null)
                    install = new CardDao();
                return install;
            }
        }
        private static Card ExtractRawData(IDataRecord dataReader)
        {
            return new Card
            {
                CardId = (Guid)dataReader["CardID"],
                PIN = dataReader["PIN"].ToString(),
                Status = (int)dataReader["Status"]
            };
        }
        public Card GetCard(string cardId)
        {
            try
            {
                var colParameter = new Collection<SqlParameter> { new SqlParameter("CardID", cardId) };
                using (var reader = SqlHelper.ExecuteReader(DBSchemas.ProcedureStores.GET_Card, colParameter))
                {
                    if (reader.Read())
                    {
                        var card = ExtractRawData(reader);

                        return card;
                    }
                    return null;
                }
            }
            catch (SqlException ex)
            {
                return null;
            }
        }
        public int ChangePin(string cardId, string newPin)
        {
            try
            {
                var colParameter = new Collection<SqlParameter> { new SqlParameter("CardID", cardId), new SqlParameter("newPin", newPin) };
                var resuft = SqlHelper.ExecuteNonQuery(DBSchemas.ProcedureStores.Change_PIN, colParameter);
                return resuft;
            }
            catch (SqlException ex)
            {
                return -1;
            }
        }
        public List<Card> GetListCard()
        {
            return null;
        }
        public int Insert(Card card)
        {
            return 0;
        }
        public int Delete(Guid cardId)
        {
            return 0;
        }
        public int Update(Card card)
        {
            return 0;
        }

        #region Withdraw
        /// <summary>
        /// Chức năng rút tiền trong thẻ tiết kiệm
        /// </summary>
        /// <param name="amount">Số tiền cần rút</param>
        /// <param name="account">Tài khoản đang thực hiện chức năng rút tiền</param>
        /// <returns></returns>
        public bool Withdraw(float amount, CheckingAccount account)
        {
            try
            {
                var colParameter = new Collection<SqlParameter> 
                { 
                    new SqlParameter("CardId", account.CardID), 
                    new SqlParameter("Amount", amount) 
                };

                var resuft = SqlHelper.ExecuteNonQuery(DBSchemas.ProcedureStores.WithdrawMoney, colParameter);
                return resuft > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        #endregion
    }
}
